package com.example.dllo.baidumusic.sql;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
*/

import android.os.Looper;

import com.example.dllo.baidumusic.bean.MyApp;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import android.os.Handler;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


import android.os.Looper;

import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by dllo on 16/9/6.
 */
public class DBTool {
    //liteOrm 对象,曾删改查方法 都操作此对象
    private LiteOrm mLiteOrm;
    //Handler 用来做线程切换,为了在主线程来执行回调,返回数据
    private Handler mHandler;

    //线程池
    ExecutorService threadPool = Executors.newFixedThreadPool(5);

    private static DBTool ourInstance = new DBTool();

    public static DBTool getInstance() {
        return ourInstance;
    }

    private DBTool() {
        //初始化liteOrm对象
        //使用MyApp里的全局Context
        //第二个参数是 数据库的名字
        mLiteOrm = LiteOrm.newSingleInstance(MyApp.getContext(), "MyDataBase.db");
        //初始化Handler
        //Handler的构造方法 参数如果传
        // Looper.getMainLooper()
        // 代表这个Handler 一定会回调到主线程
        // 属于严谨的写法

        mHandler = new Handler(Looper.getMainLooper());

    }

    //向数据库插入一个用户

    public void insertUser(final MyMusicBean myMusicBean) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                mLiteOrm.insert(myMusicBean);
            }
        });

    }

    public void set(final String s, final String a, final String e, final String y, final QueryListener queryListener) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<MyMusicBean> builder = new QueryBuilder<MyMusicBean>(MyMusicBean.class);
                builder.where(s, a);
                builder.whereAppendAnd();
                builder.whereAppend(e, y);
                ArrayList<MyMusicBean> myMusicBeen = mLiteOrm.query(builder);
                mHandler.post(new HandlerRunnable(myMusicBeen, queryListener));
            }
        });
    }

    //查询用户名的方法
    public void queryUesr(final String s, final String a, final QueryListener queryListener) {
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                QueryBuilder<MyMusicBean> builder = new QueryBuilder<MyMusicBean>(MyMusicBean.class);
                builder.where(s, a);
                ArrayList<MyMusicBean> myMusicBeen = mLiteOrm.query(builder);
                mHandler.post(new HandlerRunnable(myMusicBeen, queryListener));
            }
        });

    }


    class HandlerRunnable implements Runnable {
        ArrayList<MyMusicBean> myMusicBeen;
        QueryListener mQueryListener;

        //构造方法
        public HandlerRunnable(ArrayList<MyMusicBean> nativeBeen, QueryListener queryListener) {

            myMusicBeen = nativeBeen;
            mQueryListener = queryListener;
        }

        @Override
        public void run() {
            mQueryListener.onQueryComplete(myMusicBeen);
        }
    }

    public interface QueryListener {
        void onQueryComplete(ArrayList<MyMusicBean> localBeen);
    }

}
